/**
 * 
 */
package com.burgstaller.aGTD;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.ListAdapter;

import com.burgstaller.aGTD.utils.AbstractListBase;
import com.burgstaller.aGTD.utils.IconedTaskCursorAdapter;
import com.burgstaller.provider.AGtd;

/**
 * @author rainer
 *
 */
public class TaskList extends AbstractListBase {

	
	/** the icon for a non-finished task. */
	private Drawable m_normalTaskIcon;

	/** the icon for a finished task. */
	private Drawable m_completedTaskIcon;
	private String m_filter;
	
	/**
	 * The columns we are interested in from the database
	 */
	private static final String[] PROJECTION = new String[] {
			AGtd.Tasks._ID, 
			AGtd.Tasks.TITLE, 	// 1
			AGtd.Tasks.COMPLETED, // 2
			AGtd.Tasks.SYNC_STATUS, // 3
			AGtd.Tasks.MODIFIED_DATE // 4
	};

	private static final int COMPLETED_INDEX = 2;
	private static final int SYNC_STATUS_INDEX = 3;
	private static final int MODIFIED_INDEX = 4;

	private static final int MARK_AS_COMPLETE_ID = EDIT_ID + 2;

	@Override
	public void onCreate(Bundle icicle) {
		m_normalTaskIcon = getResources().getDrawable(R.drawable.normal_task);
		m_completedTaskIcon = getResources().getDrawable(
				R.drawable.completed_task);
		m_filter = getIntent().getStringExtra("filter");
		Log.d(AGtd.LOG_TAG, "have filter: " + m_filter);	
		super.onCreate(icicle);		
	}

	@Override
	protected Uri getListItemBaseURI() {
		return AGtd.Tasks.CONTENT_URI;
	}
	
	@Override
	protected String getSqlWhere() {
		return m_filter;
	}

	@Override
	protected String[] getProjection() {
		return PROJECTION;
	}

	@Override
	protected void addAdditionalMenus(Menu menu, Uri url) {
        Item item = menu.add(0, MARK_AS_COMPLETE_ID, 
        		R.string.menu_mark_as_complete);
        item.setShortcut('5', 'c');
	}
	
	@Override
	public boolean onOptionsItemSelected(Item item) {
		switch (item.getId()) {
		case MARK_AS_COMPLETE_ID:
			markAsCompleted();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void markAsCompleted() {
		m_cursor.moveTo(getSelectedItemPosition());
		m_cursor.updateInt(COMPLETED_INDEX, 1);
		if (m_cursor.getInt(SYNC_STATUS_INDEX) != AGtd.Tasks.SYNC_STATUS_NEW) {
			m_cursor.updateInt(SYNC_STATUS_INDEX, 
					AGtd.Tasks.SYNC_STATUS_MODIFIED);
		}
		managedCommitUpdates(m_cursor);
	}
	
	@Override
	protected String getClickAction() {
		return Intent.EDIT_ACTION;
	}
	
	@Override
	protected ListAdapter provideListAdapter() {
//		return new CheckboxTaskCursorAdapter(m_cursor, this, 2, 1,
//				m_normalTaskIcon, m_completedTaskIcon);
		return new IconedTaskCursorAdapter(m_cursor, this, 2, 1,
				m_normalTaskIcon, m_completedTaskIcon);
	}
	
	@Override
	protected int provideContentViewResourceId() {
		// disable resource ids
		return -1;
	}
	
	protected final void deleteItem() {
		m_cursor.moveTo(getSelectedItemPosition());
		// mark as deleted
		m_cursor.updateInt(SYNC_STATUS_INDEX, AGtd.Tasks.SYNC_STATUS_DELETED);
		m_cursor.updateLong(MODIFIED_INDEX, System.currentTimeMillis());
		managedCommitUpdates(m_cursor);
	}

}
